package com.office.automate.service.impl.authenticate;

import java.util.HashMap;

import com.office.automate.dataaccess.def.user.SystemLoginAuthenticationDao;
import com.office.automate.dataaccess.impl.user.SystemLoginAuthenticationDaoImplementer;
import com.office.automate.encrypt.password.EncryptPassword;
import com.office.automate.exception.authenticate.UserAuthenticationExcaptionKeys;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.login.data.validator.LoginDataValidator;
import com.office.automate.model.authenticate.ChangePassword;
import com.office.automate.model.authenticate.UserAuthenticator;
import com.office.automate.service.def.authenticate.SystemLoginAuthenticationService;

public class SystemLoginAuthenticationServiceImplementer implements SystemLoginAuthenticationService {

	private SystemLoginAuthenticationDao systemLoginAuthenticationDao = new SystemLoginAuthenticationDaoImplementer();
	private UserAuthenticator userAuthenticator = null;
	private LoginDataValidator loginDataValidator = new LoginDataValidator();

	public HashMap<String, FrontierVisibleException> validateFieldData(UserAuthenticator anUserAuthenticator) throws FrontierVisibleException {
		HashMap<String, FrontierVisibleException> fieldValidationErrors = null;

		loginDataValidator.validateLoginDataFields(anUserAuthenticator);
		if (loginDataValidator.isValidationErrorsExist()) {
			fieldValidationErrors = loginDataValidator.getFieldValidationErrors();
		}
		return fieldValidationErrors;
	}

	public void validateCredentials(UserAuthenticator anUserAuthenticator) throws FrontierVisibleException {
		userAuthenticator = systemLoginAuthenticationDao.getAuthenticationDetailsByUsername(anUserAuthenticator.getUsername());
		if (!userAuthenticator.getPassword().equalsIgnoreCase(new 
									EncryptPassword().getEncryptedPasswordMD5(anUserAuthenticator.getPassword()))) {
			throw new FrontierVisibleException(UserAuthenticationExcaptionKeys.USERNAME_OR_PASSWORD_INCORRECT, null);				
		}
	}

	public UserAuthenticator getSuccessfulAuthenticatedDetails() throws FrontierVisibleException {

		if (userAuthenticator != null) {
			return userAuthenticator;
		} else {
			throw new FrontierVisibleException(UserAuthenticationExcaptionKeys.INVALID_AUTHENTICATOR, null);
		}
	}

	public HashMap<String, FrontierVisibleException> validateChangePasswordDetails(ChangePassword changePassword) throws FrontierVisibleException {
		HashMap<String, FrontierVisibleException> fieldValidationErrors = null;
		UserAuthenticator usrAuth = systemLoginAuthenticationDao.getAuthenticationDetailsByUsername(changePassword.getUsername());
		changePassword.setCurrentSystemPassword(usrAuth.getPassword());
		loginDataValidator.validateChangePasswordDetails(changePassword);

		if (loginDataValidator.isValidationErrorsExist()) {
			fieldValidationErrors = loginDataValidator.getFieldValidationErrors();
		}
		return fieldValidationErrors;
	}

	public void changePassword(ChangePassword changePassword) throws FrontierVisibleException {
		systemLoginAuthenticationDao.changePassword(changePassword);
		
	}
}